package com.elvin.wx.controller;


import com.elvin.wx.beans.User;
import com.elvin.wx.service.UserService;
import com.elvin.wx.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RestController
@RequestMapping("/userController")
@Log4j2
@Api("用户管理")
public class UserController {

	@Resource
	private UserService service;

	@ApiOperation(value = "获取所有人员")
	@PostMapping("/queryAll")
	public Result<Object> queryAll() {
		Result<Object> result = new Result<>();
		try {
			List<User> selectList = service.selectList(null);
			result.success(selectList);
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "新增人员")
	@PostMapping("/insert")
	public Result<Object> insert(@RequestBody User entity) {
		Result<Object> result = new Result<>();
		try {
			service.insert(entity);
			result.success(entity, "新增成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "修改人员")
	@PostMapping("/update")
	public Result<Object> update(@RequestBody User entity) {
		Result<Object> result = new Result<>();
		try {
			service.updateById(entity);
			result.success("修改成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "删除人员 ")
	@PostMapping("/delete")
	public Result<Object> delete(@ApiParam("人员id") @RequestParam("id") Integer id) {
		Result<Object> result = new Result<>();
		try {
			service.deleteById(id);
			result.success("删除成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "根据ID获取人员")
	@PostMapping("/getById")
	public Result<Object> getById(@ApiParam("人员id") @RequestParam("id") Integer id) {
		Result<Object> result = new Result<>();
		try {
			User entity = new User();
			entity.setId(id);
			entity = service.selectById(entity);
			result.success(entity);
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}
}

